// @Title
// @Description
// @Author
// @Date 11/6/20 12:05 PM
package gstr

import "strings"

var macSeparators = []string{":", "-", " "}

func FormatMacAddress(input string) string {
	if input == "" {
		return input
	}
	strMacLower := strings.ToLower(input)
	var parts []string
	for _, sep := range macSeparators {
		if strings.Contains(strMacLower, sep) {
			parts = SplitAndTrim(strMacLower, sep)
			//匹配到之后不再处理后面的规则
			break
		}
	}
	var macArray []string
	if len(parts) == 3 && len(parts[0]) == 4 && len(parts[1]) == 4 && len(parts[2]) == 4 { //增加判断每个段的长度，避免数组越界
		macArray = []string{parts[0][0:2], parts[0][2:4], parts[1][0:2], parts[1][2:4], parts[2][0:2], parts[2][2:4]}
	} else if len(parts) == 6 {
		macArray = []string{"", "", "", "", "", ""}
		for index, data := range parts {
			if len(data) == 1 {
				data = "0" + data
			}
			macArray[index] = data
		}
	}

	return strings.Join(macArray, "")
}
